package com.pdstudio.carrecom.ui.activity.owner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.bean.Boutique;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.owner.News;
import com.pdstudio.carrecom.bean.owner.NewsDetail;
import com.pdstudio.carrecom.logger.Logger;
import com.pdstudio.carrecom.tools.JsonUtil;
import com.pdstudio.carrecom.ui.activity.buy.ActivityBoutique;
import com.pdstudio.carrecom.ui.adapter.owner.NewsListDetailAdapter;

/**
 * 类: ActivityBoutiqueDetail <br>
 * 描述: 我的消息详情 <br>
 * 作者: ws <br>
 * 时间: 2015-6-15 下午11:32:03
 */
public class ActivityMessageDetail extends Activity implements OnClickListener {

	private static final String TAG = "ActivityMessageDetail";

	private static final int MSG_WHAT_ABOUT = 1;

	private TextView txtTitle;
	private ImageView ivBack;

	private ResultInfo mResultInfo;

	private AppContext mAppContext = AppContext.getInstance();

	private News mBoutique;

	private List<NewsDetail> mStrings = new ArrayList<>();

	private NewsListDetailAdapter mAdapter;

	private String type;// todo:这里是int 还是 string
	private int userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_message_detail);
		initTitle();
		initView();
		initData();
	}

	@Override
	protected void onResume() {
		super.onResume();

		getBoutiques();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.i(TAG, msg);
			switch (msg.what) {
			case MSG_WHAT_ABOUT:// 关于
				if (mStrings != null) {
					if (mStrings.size() > 0) {
						mStrings = (List<NewsDetail>) msg.obj;
						mAdapter.notifyDataSetChanged();

//						initData();
					} else {
						Toast.makeText(ActivityMessageDetail.this, "没有更多数据了",
								Toast.LENGTH_SHORT).show();

					}
				}
				break;

			default:
				break;
			}
		}
	};

	private TextView mType;
	private TextView mStatus;
	private TextView mDate;
	private ListView mListView;
	private EditText mMessage;
	private Button mSubmit;
	private Button mCancel;

	private int messageId;

	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("消息详情");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(this);
	}

	private void initView() {
		mType = (TextView) findViewById(R.id.messagedetail_type);
		mStatus = (TextView) findViewById(R.id.message_status);
		mDate = (TextView) findViewById(R.id.message_date);
		mListView = (ListView) findViewById(R.id.message_list);
		mMessage = (EditText) findViewById(R.id.message_msg);
		mSubmit = (Button) findViewById(R.id.message_submit);
		mCancel = (Button) findViewById(R.id.message_cancel);

		mSubmit.setOnClickListener(this);
		mCancel.setOnClickListener(this);
	}

	private void initData() {
		Intent intent = getIntent();
		mBoutique = (News) intent.getSerializableExtra("BOUTIQUE_DETAIL");

		mType.setText(mBoutique.type);
		mStatus.setText(mBoutique.status);
		mDate.setText(mBoutique.date);

		type = mBoutique.type;
		userId = mBoutique.userId;
		messageId = mBoutique.id;

		mAdapter = new NewsListDetailAdapter(this, mStrings);
		mListView.setAdapter(mAdapter);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		}
	}

	/**
	 * 
	 * 方法: getBoutiques <br>
	 * 描述: 获取精品列表 <br>
	 * 作者: ws <br>
	 * 时间: 2015-6-16 上午12:27:40
	 */
	private void getBoutiques() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mNewCarBoutiqueListener);
			String url = ServiceHelper.OwnerMessageDetail;
			Map<String, Object> rps = new HashMap<String, Object>();
			
			int typeid = 0;
			switch (type) {
			case "买车预约":
				typeid=0;
				break;
			case "试驾预约":
				typeid=1;
				break;
			case "二手车评估":
				typeid=2;
				break;
			case "二手车置换":
				typeid=3;
				break;
			case "租车预约":
				typeid=4;
				break;
			case "保险预约":
				typeid=5;
				break;
			case "维保预约":
				typeid=6;
				break;
			case "最新活动":
				typeid=7;
				break;
			case "违章":
				typeid=8;
				break;
			case "个人留言":
				typeid=9;
				break;
			default:
				break;
			}

			rps.put("uid", mAppContext.getUserId());
			rps.put("type", typeid);
			rps.put("userId", userId);
//			rps.put("messageId", messageId);
			Logger.i(url);
			http.get(url, rps);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mNewCarBoutiqueListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			Logger.json(result);
			ResultInfo resultInfo = null;
			try {
				resultInfo = new Gson().fromJson(result,
						new TypeToken<ResultInfo>() {
						}.getType());
				if ((resultInfo != null)
						&& (resultInfo.msg
								.equals(ServiceHelper.msg_code_sucess))) {
					try {
						mResultInfo = resultInfo;
						Logger.i("天气", resultInfo);

						mStrings = JsonUtil.fromJsonArray(
								JsonUtil.toJson(mResultInfo.data),
								NewsDetail.class);

						mAdapter.notifyDataSetChanged();
						Message msg = new Message();
						msg.what = MSG_WHAT_ABOUT;
						mHandler.sendMessage(msg);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int error, String msg) {

		}

		@Override
		public void onCancel() {

		}
	};

}
